﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Ken_WPF_UI.View
{
    /// <summary>
    /// CalendarWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CalendarWindow : Window
    {
        public CalendarWindow()
        {
            InitializeComponent();
        }

        private void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {

            //时间由近及远排序  使用this.myCalendar.SelectedDates的last 和first 方法 是取不到有序的值
            List<DateTime> list = this.myCalendar.SelectedDates.OrderBy((date1) =>
             {
                 return date1.Date;
             }).ToList();
            this.txtStart.Text = list[0].ToShortDateString();
            this.txtEnd.Text = list[list.Count - 1 > 0 ? list.Count - 1 : 0].ToShortDateString();
            Console.WriteLine("-----");
        }

        /// <summary>
        /// 需要点击两次  如果 不给Calendar控件设置 PreviewMouseUp 这个事件（具体方法myCalendar_PreviewMouseUp）需要点击两次才能获取值 原因在于在刚开始button没有获得焦点 需要把鼠标焦点在日历控件的时候 让鼠标先失去焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {

            //时间由近及远排序  使用this.myCalendar.SelectedDates的last 和first 方法 是取不到有序的值
            List<DateTime> list = this.myCalendar.SelectedDates.OrderBy((date1) =>
            {
                return date1.Date;
            }).ToList();

            if (list.Count <= 0)
            {
                return;
            }


            this.txtStart.Text = list[0].ToShortDateString();
            this.txtEnd.Text = list[list.Count - 1 > 0 ? list.Count - 1 : 0].ToShortDateString();
            Console.WriteLine("-----");
        }

        /// <summary>
        /// 预 鼠标弹起 事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void myCalendar_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if(Mouse.Captured is CalendarItem)
            {
                Mouse.Capture(null);
            }
        }
    }
}
